/** \file    MaskSaveDlg.cpp
  * \brief   Specialized file save dialog for bit mask that can be exported in two formats.
  * \date    2002 - 2004
  * \author  Pavel Agapov
  * \author  Fedor Chelnokov
  * \version $Header: /home/cscom1r4/cvsroot/cs/Claxa/src/MaskSaveDlg.cpp,v 1.1 2004/07/13 14:12:22 cscom1r4 Exp $
  */

#include "stdafx.h"
#include <Dlgs.h> //for pspHelp definition
#include "Resource.h"
#include "MaskSaveDlg.h"
						 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_DYNAMIC(MaskSaveDlg, CFileDialog)

///mysterious function, I don't understand a word in it
UINT CALLBACK MaskSaveDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	if (hWnd == NULL)
		return 0;

	_AFX_THREAD_STATE * pThreadState = ::AfxGetThreadState();
	if (pThreadState->m_pAlternateWndInit != NULL)	{
		ASSERT_KINDOF(CFileDialog, pThreadState->m_pAlternateWndInit);
		pThreadState->m_pAlternateWndInit->SubclassWindow(hWnd);
		pThreadState->m_pAlternateWndInit = NULL;
	}
	ASSERT(pThreadState->m_pAlternateWndInit == NULL);

	if (message == WM_COMMAND && LOWORD(wParam) == pshHelp)	{
		SendMessage(hWnd, WM_COMMAND, ID_HELP, 0);
		return 1;
	}

	MaskSaveDlg * pDlg = (MaskSaveDlg *) CWnd::FromHandlePermanent(hWnd);
	ASSERT(pDlg != NULL);
	ASSERT_KINDOF(MaskSaveDlg, pDlg);

	if (message == WM_COMMAND)
		pDlg->OnCustomCommand(LOWORD(wParam), HIWORD(wParam));

	return 0;
}

MaskSaveDlg::MaskSaveDlg(LPCTSTR lpszDefExt, LPCTSTR lpszFilename, LPCTSTR lpszFilter, bool allow_greyscale_box, CWnd* pParentWnd) :
	CFileDialog(FALSE, lpszDefExt, lpszFilename, 
		OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR | OFN_ENABLESIZING | //| OFN_DONTADDTORECENT (undefined symbol?)
			(allow_greyscale_box ? (OFN_EXPLORER | OFN_ENABLETEMPLATE) : 0),
		lpszFilter, pParentWnd)
{
	if (allow_greyscale_box) {
		m_ofn.Flags |= OFN_ENABLESIZING; //this flag is removed in CFileDialog ctor when it sees OFN_ENABLETEMPLATE flag
		//m_ofn.FlagsEx = OFN_EX_NOPLACESBAR;
		SetTemplate(0, IDD_FILESAVE_GREYSCALE_ADDON);
		m_ofn.lpfnHook = MaskSaveDlgProc;
	}
	m_bSaveInGreyscale = false;
}

BEGIN_MESSAGE_MAP(MaskSaveDlg, CFileDialog)
	//{{AFX_MSG_MAP(MaskSaveDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void MaskSaveDlg::OnInitDone()
{
	CFileDialog::OnInitDone();

	CButton * pForce = (CButton *) GetDlgItem(IDC_SAVE_IN_GREYSCALE);
	if (pForce)
		pForce->SetCheck(m_bSaveInGreyscale ? 1 : 0);
}

void MaskSaveDlg::OnCustomCommand(UINT nID, UINT msg)
{
	CButton * pForce = (CButton *) GetDlgItem(IDC_SAVE_IN_GREYSCALE);
	if (pForce)
		m_bSaveInGreyscale = pForce->GetCheck() == 1;
}
